package com.zhuhjay.boot.service.impl;

import com.alibaba.dubbo.config.annotation.Service;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.zhuhjay.boot.mapper.OrdersettingMapper;
import com.zhuhjay.domain.entity.OrderSetting;
import com.zhuhjay.service.OrdersettingService;
import org.springframework.transaction.annotation.Transactional;

import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.*;

/**
 * <p>
 * ԤԼ 服务实现类
 * </p>
 *
 * @author zhuhjay
 * @since 2022-04-02
 */
@Service(interfaceClass = OrdersettingService.class)
@Transactional
public class OrdersettingServiceImpl extends ServiceImpl<OrdersettingMapper, OrderSetting> implements OrdersettingService {

    @Override
    public void add(List<String[]> list) {
        if( list != null && !list.isEmpty() ){
            list.forEach(item -> {
                String orderDate = item[0];
                String number = item[1];
                // 判断该日期是否设置了预约
                Integer count = baseMapper.selectCount(
                        new QueryWrapper<OrderSetting>()
                                .eq("orderDate", orderDate)
                );
                // 修改
                if( count > 0 ){
                    baseMapper.update(null,
                            new UpdateWrapper<OrderSetting>()
                                    .eq("orderDate", orderDate)
                                    .set("number", number)
                    );
                }
                // 新增
                else{
                    OrderSetting orderSetting = new OrderSetting(new Date(orderDate), Integer.parseInt(number));
                    baseMapper.insert(orderSetting);
                }
            });
        }
    }

    @Override
    public List<Map<String, Object>> getOrdersettingByMonth(String date) {
        // 模糊查询当月的数据
        QueryWrapper<OrderSetting> queryWrapper = new QueryWrapper<>();
        queryWrapper.like("orderDate", date + "%");
        List<OrderSetting> orderSettings = baseMapper.selectList(queryWrapper);
        List<Map<String, Object>> mapList = new ArrayList<>();
        if( !orderSettings.isEmpty() ){
            orderSettings.forEach(orderSetting -> {
                Map<String, Object> map = new HashMap<>();
                map.put("date", orderSetting.getOrderDate().getDate());
                map.put("number", orderSetting.getNumber());
                map.put("reservations", orderSetting.getReservations());
                mapList.add(map);
            });
        }
        return mapList;
    }

    @Override
    public void editNumberByDate(OrderSetting orderSetting) {
        DateFormat df1 = new SimpleDateFormat("yyyy-MM-dd");
        String orderDate = df1.format(orderSetting.getOrderDate());
        // 查找该日期是否已经设置
        Integer count = baseMapper.selectCount(
                new QueryWrapper<OrderSetting>()
                        .eq("orderDate", orderDate)
        );
        // 修改
        if( count > 0 ){
            UpdateWrapper<OrderSetting> updateWrapper = new UpdateWrapper<>();
            updateWrapper
                    .eq("orderDate", orderDate)
                    .set("number", orderSetting.getNumber());
            baseMapper.update(null, updateWrapper);
        }
        // 新增
        else{
            baseMapper.insert(orderSetting);
        }

    }
}
